package com.cn.transfer.jobhandler;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.VvSaSaleOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.mapper.VvSaSaleOrderMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: weidian
 * @date: 2023/10/30 14:05
 * @Description: 销售订单是否审核，未审核提醒
 */
@Slf4j
@Component
public class SaleOrderAuditXxlJob {

    @Autowired
    private VvSaSaleOrderMapper vvSaSaleOrderMapper;

    @XxlJob("SaleOrderAudit")
    public void saleOrderAudit() {
        List<VvSaSaleOrder> vvSaSaleOrders = vvSaSaleOrderMapper.byAudit();
        for (VvSaSaleOrder vvSaSaleOrder : vvSaSaleOrders) {
            String gettoken = ConnmeUtils.gettoken();
            Header[] originalHeaders = new Header[1];
            originalHeaders[0] = new BasicHeader("Content-Type", "application/json;charset=utf-8");
            Map<String, Object> parameter = new HashMap<>();
            String makerAddress = vvSaSaleOrder.getMakerAddress();
            String salesperson = vvSaSaleOrder.getSalesperson();
            String touser = "";
            if (!makerAddress.equals("") && !salesperson.equals("")) {
                touser = makerAddress + "|" + salesperson;
            } else {
                touser = makerAddress + salesperson;
            }
            parameter.put("touser", touser);
            parameter.put("msgtype", "text");
            parameter.put("agentid", "1000114");
            Map<String, Object> textContent = new HashMap<>();
            textContent.put("content", "您的销售订单一天还未审核，单号："+vvSaSaleOrder.getDocumentnumber()+" 请更进");
            parameter.put("text", textContent);
            String jsonSale = JSONUtil.toJsonStr(parameter);
            JSONObject jdySale = ApiClient.sendPostRequest("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + gettoken, originalHeaders, jsonSale);
            log.info("销售订单审核提醒==="+jdySale);
        }
    }

}
